package com.example.demo.controller;

import com.example.demo.component.AjaxResult;
import com.example.demo.config.AppValue;
import com.example.demo.config.EncrypTool;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.java2d.pipe.AAShapePipe;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    @RequestMapping("/reg")
    public AjaxResult reg(UserInfo userInfo) {
        //非空校验
        if(userInfo == null || !StringUtils.hasLength(userInfo.getPassword()) ||
        !StringUtils.hasLength(userInfo.getUsername())) {
            return AjaxResult.error(-1, "参数错误1");
        }
        //检验此账号是否注册过
        int count = userService.selectUserCount(userInfo.getUsername());
        if(count != 0) {
            return AjaxResult.error(-1, "此用户名已存在，请重试!");
        }
        //密码加盐
        userInfo.setPassword(EncrypTool.encryp(userInfo.getPassword()));
        return AjaxResult.success(userService.addUserInfo(userInfo));
    }

    @RequestMapping("/login")
    public AjaxResult login(HttpServletRequest request, String username, String password) {
        //非空校验
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return AjaxResult.error(-1, "参数错误2");
        }
        //检验用户名密码是否异常
        UserInfo userInfo = userService.selectUserInfoByUsername(username);
        if(userInfo == null || !StringUtils.hasLength((userInfo.getUsername())) ||
        !StringUtils.hasLength(userInfo.getPassword())) {
               return AjaxResult.error(-1, "参数错误3");
        }
        //检验密码是否正确
        if(EncrypTool.check(password, userInfo.getPassword())) {
            //登录成功，记录 session 信息
            HttpSession session = request.getSession(true);
            session.setAttribute(AppValue.SESSION_KEY, userInfo);
           return AjaxResult.success(1);
        }
        return AjaxResult.error(-1, "参数错误4");
    }

    @RequestMapping("/cur-user")
    public AjaxResult getCurUserInfo(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(AppValue.SESSION_KEY) == null) {
            return AjaxResult.error(-1, "参数错误5");
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(AppValue.SESSION_KEY);
        return AjaxResult.success(userInfo);
    }

//    @RequestMapping("/msg_cache")
//    @Cacheable(value = "spring.cache", key = "#username+#msg")
//    public AjaxResult cacheMsg(String username, String msg) {
//        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(msg)) {
//            return AjaxResult.error(-1);
//        }
//        return AjaxResult.success(1);
//    }


}
